package com.fly.base.service.impl;

import com.fly.base.dao.LabelDao;
import com.fly.base.pojo.Label;
import com.fly.base.service.LabelService;
import entity.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class LabelServiceImpl implements LabelService {
    @Autowired
    private LabelDao labelDao;
    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部标签
     *
     * @return
     */
    @Cacheable(cacheNames = "base_", key = "#id")
    public List<Label> findAll(String id) {
        return labelDao.findAll();
    }

    /**
     * 根据ID查询标签
     *
     * @return
     */
    @Cacheable(cacheNames = "base_", key = "#id")
    public Label findById(String id) {
        return labelDao.findById(id).get();
    }

    /**
     * 增加标签
     *
     * @param label
     */
    @CachePut(cacheNames = "base_", key = "#id")
    public Object add(Label label, String id) {
        label.setId(id);//设置ID
        labelDao.save(label);
        return label;
    }

    /**
     * 修改标签
     *
     * @param label
     */
    @CachePut(cacheNames = "base_", key = "#id")
    public Object update(Label label, String id) {
//        再设置一下id不然就会报错，别问我我也不知道为啥
        label.setId(id);
         labelDao.save(label);
         return this.labelDao.findById(id);
    }

    /**
     * 删除标签
     *
     * @param id
     */
    @CacheEvict(cacheNames = "base_", key = "#id")
    public void deleteById(String id) {
        labelDao.deleteById(id);
    }

    /*
    条件查询
     */
    public List<Label> findSearch(Label label) {
        return labelDao.findAll(new Specification<Label>() {

            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {

                List<Predicate> predicateList = new ArrayList<>();
                if (label.getLabelname() != null && !"".equals(label.getLabelname())) {
                    predicateList.add(cb.like(root.get("labelname").as(String.class), "%" + label.getLabelname() + "%"));
                }
                if (label.getState() != null && !"".equals(label.getState())) {
                    predicateList.add(cb.equal(root.get("state").as(String.class), label.getState()));
                }
                if (label.getRecommend() != null && !"".equals(label.getRecommend())) {
                    predicateList.add(cb.equal(root.get("recommend").as(String.class), label.getRecommend()));
                }
                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        });
    }

    /*
    分页版条件查询
     */
    public Page<Label> findSearch(Label label, int page, int size) {
        Specification specification = new Specification<Label>() {

            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {

                List<Predicate> predicateList = new ArrayList<>();
                if (label.getLabelname() != null && !"".equals(label.getLabelname())) {
                    predicateList.add(cb.like(root.get("labelname").as(String.class), "%" + label.getLabelname() + "%"));
                }
                if (label.getState() != null && !"".equals(label.getState())) {
                    predicateList.add(cb.equal(root.get("state").as(String.class), label.getState()));
                }
                if (label.getRecommend() != null && !"".equals(label.getRecommend())) {
                    predicateList.add(cb.equal(root.get("recommend").as(String.class), label.getRecommend()));
                }
                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return labelDao.findAll(specification, pageRequest);
    }
}
